package nothing.model;

import org.appfuse.model.BaseObject;

import java.math.BigDecimal;
import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.appfuse.model.User;

@Entity
@Table(name = "purchase")
public class Purchase extends BaseObject {

    private Long id;
    private Food food;
    private Equipment equipment;
    private Shop shop;
    private BigDecimal cost;
    private Currency currency;
    private Measurement measurement;
    private Date when;
    private BigDecimal qty;
    private User user;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false, insertable = true, updatable = true)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setFood(Food food) {
        this.food = food;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "food_id", referencedColumnName = "id", nullable = true, insertable = true, updatable = true)
    public Food getFood() {
        return food;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "equipment_id", referencedColumnName = "id", nullable = true, insertable = true, updatable = true)
    public Equipment getEquipment() {
        return equipment;
    }

    public void setEquipment(Equipment equipment) {
        this.equipment = equipment;
    }

    public void setShop(Shop shop) {
        this.shop = shop;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "shop_id", referencedColumnName = "id", nullable = false, insertable = true, updatable = true)
    public Shop getShop() {
        return shop;
    }

    public void setCost(BigDecimal cost) {
        this.cost = cost;
    }

    @Column(name = "cost", length = 6)
    public BigDecimal getCost() {
        return cost;
    }

    public void setMeasurement(Measurement measurement) {
        this.measurement = measurement;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "measurement_id", referencedColumnName = "id", nullable = false, insertable = true, updatable = true)
    public Measurement getMeasurement() {
        return measurement;
    }

    public void setCurrency(Currency currency) {
        this.currency = currency;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "currency_id", referencedColumnName = "id", nullable = false, insertable = true, updatable = true)
    public Currency getCurrency() {
        return currency;
    }

    @Column(name = "dtm", length = 19)
    public Date getWhen() {
        return when;
    }

    public void setWhen(Date when) {
        this.when = when;
    }

    public void setQty(BigDecimal qty) {
        this.qty = qty;
    }

    @Column(name = "qty", length = 19)
    public BigDecimal getQty() {
        return qty;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((cost == null) ? 0 : cost.hashCode());
        result = prime * result + ((currency == null) ? 0 : currency.hashCode());
        result = prime * result + ((equipment == null) ? 0 : equipment.hashCode());
        result = prime * result + ((food == null) ? 0 : food.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((qty == null) ? 0 : qty.hashCode());
        result = prime * result + ((shop == null) ? 0 : shop.hashCode());
        result = prime * result + ((measurement == null) ? 0 : measurement.hashCode());
        result = prime * result + ((when == null) ? 0 : when.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Purchase other = (Purchase) obj;
        if (cost == null) {
            if (other.cost != null) {
                return false;
            }
        } else if (!cost.equals(other.cost)) {
            return false;
        }
        if (currency == null) {
            if (other.currency != null) {
                return false;
            }
        } else if (!currency.equals(other.currency)) {
            return false;
        }
        if (equipment == null) {
            if (other.equipment != null) {
                return false;
            }
        } else if (!equipment.equals(other.equipment)) {
            return false;
        }
        if (food == null) {
            if (other.food != null) {
                return false;
            }
        } else if (!food.equals(other.food)) {
            return false;
        }
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (qty == null) {
            if (other.qty != null) {
                return false;
            }
        } else if (!qty.equals(other.qty)) {
            return false;
        }
        if (shop == null) {
            if (other.shop != null) {
                return false;
            }
        } else if (!shop.equals(other.shop)) {
            return false;
        }
        if (measurement == null) {
            if (other.measurement != null) {
                return false;
            }
        } else if (!measurement.equals(other.measurement)) {
            return false;
        }
        if (when == null) {
            if (other.when != null) {
                return false;
            }
        } else if (!when.equals(other.when)) {
            return false;
        }
        return true;
    }

    public String toString() {
        return "purchase{" +
                "id=" + id + ", " +
                "food=" + food + ", " +
                "shop=" + shop + ", " +
                "cost=" + cost + ", " +
                "currency=" + currency + ", " +
                "measurement=" + measurement + ", " +
                "when=" + when + ", " +
                "qty=" + qty +
                "}";
    }
}
